package com.hopu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hopu.pojo.Dengji;
import com.hopu.pojo.Emp;
import com.hopu.pojo.House;
import com.hopu.service.DjrzService;
import com.hopu.util.DateFormatGetTime;
import com.hopu.util.FileUtil;
import com.hopu.util.ResultUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@RequestMapping("/djrz")
@RestController
public class DjrzController {
/*
* 登记入住
* */

    @Resource
    private DjrzService djrzServiceImpl;

    @PostMapping("/findAllDjrz")
    private ResultUtils findAllDjrz(@RequestParam(value = "current",defaultValue = "1") Integer current){

        try {
            PageHelper.startPage(current,6);
            List<Dengji> allDjrz = djrzServiceImpl.findAllDjrz();

            PageInfo pageInfo = new PageInfo(allDjrz);
            return new ResultUtils(200,"查询页面成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtils(9999,"查询页面失败",null);
        }
    }

    @PostMapping("addDjrz")
    public ResultUtils addDj(
            HttpServletRequest request,HttpServletResponse response,
            MultipartHttpServletRequest multipartRequest,
            @RequestParam(value = "cid",required = true)int cid,
            @RequestParam(value = "hid",required = true)int hid,
            @RequestParam(value = "myj",required = true)Float myj,
            @RequestParam(value = "myzj",required = true)Float myzj,
            @RequestParam(value = "mbegintimee")String mbegintimee
                             ){
        try {
            // 下次收租时间
            long mbegintime = DateFormatGetTime.getTime(mbegintimee);

            Iterator<String> it = multipartRequest.getFileNames();
            String filePath = "";
            while (it.hasNext()){
                filePath = FileUtil.upload(multipartRequest.getFile((String) it.next()));
            }

            Emp empLog = (Emp) request.getSession().getAttribute("empLog");

            int num = djrzServiceImpl.addDjrz(new Date().getTime(),empLog.getEid(),cid,hid,filePath,myj,myzj,0,mbegintime);

            if (num==0){
                return new ResultUtils(400,"ERROR",null);
            }
            return new ResultUtils(200,"SUCCESS",1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("uppDjrz")
    public ResultUtils uppDjrz(Dengji dengji, String mbegintimee,
                               MultipartHttpServletRequest multipartRequest,
                               HttpSession session){
        try {
            // 获取session 当前登录账号信息
            Emp empLog = (Emp)session.getAttribute("empLog");
            dengji.setEid(empLog.getEid());

            dengji.setMbegintime(DateFormatGetTime.getTime(mbegintimee));
            Iterator<String> it = multipartRequest.getFileNames();
            String filePath = "";
            while (it.hasNext()){
                filePath = FileUtil.upload(multipartRequest.getFile((String) it.next()));
            }
            dengji.setMimg(filePath);
            int dj1 = djrzServiceImpl.uppDjrz(dengji);
            if (dj1<0){
                return new ResultUtils(400,"ERROR",null);
            }
            return new ResultUtils(200,"SUCCESS",1);
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    @PostMapping("findDjByMid")
    public ResultUtils findDjByMid(int mid){
        Dengji dengji = djrzServiceImpl.findDjByMid(mid);
//        System.out.println(dengji);
        return new ResultUtils(200,"查询单条数据成功",dengji);
    }

    @PostMapping("findAllDjrzByCondition")
    public ResultUtils findAllDjrzByCondition(House house,Dengji dengji, @RequestParam(value = "current",defaultValue = "1") Integer current){

        try {
            PageHelper.startPage(current,6);
            dengji.setHouse(house);
            List<Dengji> allDjrzByCondition = djrzServiceImpl.findAllDjrzByCondition(dengji);

            PageInfo pageInfo = new PageInfo(allDjrzByCondition);
            return new ResultUtils(200,"查询页面成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtils(9999,"查询页面失败",null);
        }

    }
    @PostMapping("findAllMydjOnDaoqi")
    public ResultUtils findAllMydjOnDaoqi(Dengji dengji,@RequestParam(value = "current",defaultValue = "1") Integer current){

        try {
            PageHelper.startPage(current,6);
            List<Dengji> allDaoqi = djrzServiceImpl.findAllMydjOnDaoqi(dengji);

            PageInfo pageInfo = new PageInfo(allDaoqi);
            return new ResultUtils(200,"查询页面成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtils(9999,"查询页面失败",null);
        }

    }
}
